// pages/order/ordersubmit/index.js
var server = require('../../../utils/server');
var tp;
var pay_points;
var points_rate;
Page({
    data: { use_money: 0, use_point: 0, check: ['true', ''], "coupon": [], cv: '请选择优惠劵', cpos: -1, "couponCode": '',          cartIds:'',
        sum:0,valueshow:'',
     },
    addressSelect: function () {
        wx.navigateTo({
            url: '../../address/select/index'
        });
    },
    bindChange: function (e) {

        var use_money = e.detail.value;

        this.setData({
            use_money: use_money,
        });
    },
  radioChange:function(e){
    console.log(e)
    this.setData({
      valueshow: e.detail.value
    })
  },
    bindChangeOfcoupon: function (e) {
        var couponCode = e.detail.value;

        this.setData({
            couponCode: couponCode,
        });
    },
    bindChangeOfPoint: function (e) {
        var use_point = e.detail.value;
        this.setData({
            use_point: use_point,
        });
    },
    bindPickerChange: function (e) {
        var value = e.detail.value;
        var cv = this.data.coupon[value];
        this.setData({ cv: cv, cpos: value });



        this.useCoupon();


    },
    useCoupon: function () {
        if (this.data.cpos == -1)
            return;

        var use_money = this.data.use_money;
        use_money = parseInt(use_money);
        if (use_money < 0) {
            use_money = 0;
        }
        if (isNaN(use_money)) {
            use_money = 0;
        }
        var user_money = getApp().globalData.userInfo.user_money;
        if (user_money < use_money) {
            use_money = 0;
        }


        var use_point = this.data.use_point;
        use_point = parseInt(use_point)

        if (use_point < 0) {
            use_point = 0;
        }
        if (isNaN(use_point)) {
            use_point = 0;
        }

        use_point = use_point - use_point % parseInt(points_rate);
        var user_point = pay_points;
        if (parseInt(user_point) < use_point) {
            use_point = 0;
        }
        use_point = use_point - use_point % parseInt(points_rate);



        var money = this.data.couponList[this.data.cpos].money;
        var totalObj = this.data.totalPrice;
        //totalObj.total_fee = totalObj.total_fee - money

        var m = tp - use_money;
        m = m - use_point / parseInt(points_rate);
        m = m - money;
        totalObj.total_fee = m

        if (totalObj.total_fee < 0)
            totalObj.total_fee = 0;
        this.setData({ totalPrice: totalObj });
    },
    use: function () {
        //totalPrice:
        var user_money = getApp().globalData.userInfo.user_money;
        var use_money = this.data.use_money;
        if (use_money == "0" || use_money == 0 || use_money == "" || use_money == undefined) {
            wx.showToast({
                title: '请输入余额',
                duration: 1000
            });
            return;
        }
        user_money = parseFloat(user_money)
        use_money = parseFloat(use_money)

        var use_point = this.data.use_point;
        use_point = parseInt(use_point)




        if (use_money < 0) {
            wx.showToast({
                title: '请输入大于0元的金额',
                duration: 1000
            });
            return;
            return;
        }
        if (isNaN(use_money)) {
            wx.showToast({
                title: '输入余额有误',
                duration: 1000
            });
            return;
        }
        if (use_point < 0) {
            use_point = 0;
        }
        if (isNaN(use_point)) {
            use_point = 0;
        }

        use_point = use_point - use_point % parseInt(points_rate);
        var user_point = pay_points;
        if (parseInt(user_point) < use_point) {
            use_point = 0;
        }


        if (user_money < use_money) {
            var totalObj = this.data.totalPrice;





            use_point = use_point - use_point % parseInt(points_rate);
            var m = tp - use_point / parseInt(points_rate)
            totalObj.total_fee = m
            this.setData({ totalPrice: totalObj });

            this.useCoupon();

            this.setData({ use_money: 0 });
            wx.showToast({
                title: '请输入小余当前余额',
                duration: 1000
            });
            return;
        }

        use_point = use_point - use_point % parseInt(points_rate);
        var m = tp - use_point / parseInt(points_rate)

        var totalPrice = m - use_money;
        if (totalPrice < 0)
            totalPrice = 0;
        var totalObj = this.data.totalPrice;
        totalObj.total_fee = totalPrice
        this.setData({ totalPrice: totalObj });

        this.useCoupon();
    },
    use_point: function () {
        //totalPrice:
        var user_point = pay_points;
        var use_point = this.data.use_point;

        if (use_point == "0" || use_point == 0 || use_point == "" || use_point == undefined) {
            wx.showToast({
                title: '请输入积分',
                duration: 1000
            });
            return;
        }


        use_point = parseInt(use_point)

        if (use_point < 0) {
            wx.showToast({
                title: '请输入大于0元的积分',
                duration: 1000
            });
            return;
            return;
        }
        if (isNaN(use_point)) {
            wx.showToast({
                title: '输入积分有误',
                duration: 1000
            });
            return;
        }

        var use_money = this.data.use_money;
        use_money = parseInt(use_money);
        if (use_money < 0) {
            use_money = 0;
        }
        if (isNaN(use_money)) {
            use_money = 0;
        }
        var user_money = getApp().globalData.userInfo.user_money;
        if (user_money < use_money) {
            use_money = 0;
        }


        if (parseInt(user_point) < use_point) {

            var totalObj = this.data.totalPrice;
            var m = tp - use_money;
            totalObj.total_fee = m
            this.setData({ totalPrice: totalObj });

            this.setData({ use_point: 0 });
            this.useCoupon();
            wx.showToast({
                title: '请输入小余当前积分',
                duration: 1000
            });
            return;
        }

        use_point = use_point - use_point % parseInt(points_rate);
        var m = tp - use_money;
        var totalPrice = m - (use_point / parseInt(points_rate));
        if (totalPrice < 0)
            totalPrice = 0;
        var totalObj = this.data.totalPrice;
        totalObj.total_fee = totalPrice
        this.setData({ totalPrice: totalObj });
        this.useCoupon();
    },
    onShow: function () {
        // var app = getApp();
        // var cartIds = app.globalData.cartIds;
        // var amount = app.globalData.amount;
        // this.setData({ cartIds: cartIds, amount: amount });



        // this.getCarts(cartIds);
        // 页面初始化 options为页面跳转所带来的参数
        this.getaddr();
    },
    initData: function () {
        var app = getApp();
        pay_points = app.globalData.userInfo.pay_points;
        var user_money = app.globalData.userInfo.user_money;
        this.setData({ freemoney: user_money, pay_points: pay_points });
    },
    formSubmit: function (e) {
        // user 
        var address_id = this.data.address.address_id
        var user_id = getApp().globalData.userInfo.user_id
        var use_money = this.data.use_money
        var pay_points = this.data.use_point
        var that = this;
        var app = getApp();
        var couponTypeSelect = this.data.check[0] == "true" ? 1 : 2;
        var coupon_id = 0;
        if (this.data.cpos != -1) {
            coupon_id = this.data.couponList[this.data.cpos].id;
        }
        var couponCode = this.data.couponCode;

        server.getJSON('/Cart/cart3/act/submit_order/user_id/' + user_id + "/address_id/" + address_id + "/user_money/" + use_money + "/pay_points/" + pay_points + "/couponTypeSelect/" + couponTypeSelect + "/coupon_id/" + coupon_id + "/couponCode/" + couponCode, function (res) {


            if (res.data.status != 1) {
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000
                });
                return;
            }

            var result = res.data.result
            app.globalData.wxdata = res.data.data
            app.globalData.order = res.data.order
            if (res.data.status == 1) {


                wx.showToast({
                    title: '提交成功',
                    duration: 2000
                });



                setTimeout(function () {
                    if (res.data.order.pay_status == 1) {
                        wx.switchTab({
                            url: "../../member/index/index"
                        });
                        return;
                    }
                    wx.navigateTo({
                        url: '../payment/payment?order_id=' + result
                    });
                }, 2000);

            }


        });
    },

    getCarts: function (cartIds) {
        var user_id = getApp().globalData.userInfo.user_id
        var that = this
        var app = getApp()

        server.getJSON('/Cart/cart2/user_id/' + user_id, function (res) {
            if (res.data.status == -2) {
                wx.navigateBack({
                    delta: 1, // 回退前 delta(默认为1) 页面
                    success: function (res) {
                        // success
                    },
                    fail: function (res) {
                        // fail
                    },
                    complete: function (res) {
                        // complete
                    }
                })
                return;
            }
            var user_data = app.globalData.userInfo;
            user_data.user_money = res.data.result.userInfo.user_money;
            user_data.pay_points = res.data.result.userInfo.pay_points;
            app.globalData.userInfo = user_data
            var address = res.data.result.addressList
            var cartList = res.data.result.cartList

            var userInfo = res.data.result.userInfo
            var totalPrice = res.data.result.totalPrice
            tp = totalPrice.total_fee
            points_rate = res.data.result.points
            that.setData({ address: address, cartList: cartList, userInfo: userInfo, totalPrice: totalPrice });

            var couponList = res.data.result.couponList
            var ms = that.data.coupon
            for (var i in couponList) {
                ms.push(couponList[i].name);
            }
            that.setData({ coupon: ms, couponList: couponList });
            that.initData();
        })
    },
    check1: function () {
        this.setData({ check: ['true', ''] });
    },
    check2: function () {
        this.setData({ check: ['', 'true'] });
    },
    onReady: function () {
        // 页面渲染完成
    },

    onHide: function () {
        // 页面隐藏
    },
  goshengming:function(){
    // console.log(1)
    wx.navigateTo({
      url: '../shenming/index',
    })
  },
    onLoad(option) {
        // var app = getApp();
        let options = JSON.parse(option.data);
        // var cartIds = options.cart_id;
        //   var amount = app.globalData.amount;
        //   this.setData({ cartIds: cartIds, amount: amount });
        // this.setData({ cartIds: cartIds });
        let _params = {
            username: options.username,
            token: options.token,
            cart_id: options.cart_id,
        }
        this.setData({
            cart_id:_params.cart_id
        })
        // this.getCarts(_params);
        this.getcarjsinfo(_params);
        // 页面初始化 options为页面跳转所带来的参数
    },
    onUnload: function () {
        // 页面关闭
    },
    getcarjsinfo(params){
        // server.postJSON('../cart/cart3',{
        //     cart_id: params.cart_id
        // },res=>{
        //     if(res.data.status==200){
                
        //     }
        // })
        let _this = this;
        try{
            server.send('/cart/cart3', 'POST', {
                username: getApp().globalData.username,
                token: getApp().globalData.token,
                cart_id: params.cart_id
            }).then(res=>{
                if (res.status == 200) {
                    _this.setData({
                        cartList: res.list
                    });
                    this.getsum();
                    this.getaddr();
                  let index = res.list[0].seller_info.id
                  // console.log(res)
                  this.getstatus(index)
                }
            })
        }catch(error){
            console.log(error)
        }
        
    },
    getstatus:function(index){
      let that = this
      server.send('/common/change-endtime-user', 'POST', {
        shop_id: index
      }).then(res => {
        // console.log(res)
        that.setData({
          status: res.list.tenant.status
        })
      })
    },
    getsum() {
        let _this = this;
        let sum = 0;
        this.data.cartList.map((item, index) => {
            sum = item.order_amount * 1 + sum + item.yunfei * 1;
        })
        this.setData({
            sum:sum
        })
    },
    getaddr(){
        let _this= this;
        // server.postJSON('/person/address',{
        //     username:getApp().globalData.username,
        //     token: getApp().globalData.token,
        // },res=>{
        //     if(res.data.status==200){
        //         if(res.data.list.length>0){
        //             res.data.list.map(item=>{
        //                 if (item.is_default==1){
        //                     _this.setData({
        //                         address:item
        //                     })
        //                 }
        //             })
        //         }
                
        //     }
        // })
        server.send('/person/address','POST', {
            username: getApp().globalData.username,
            token: getApp().globalData.token,
        }).then(res => {
            if (res.status == 200) {
                if (res.list.length > 0) {
                    res.list.map(item => {
                        if (item.is_default == 1) {
                            _this.setData({
                                address: item
                            })
                        }
                    })
                }

            }
        })
    },
    submit(){
      let status = this.data.status
        let _params={
            username:getApp().globalData.username,
            token: getApp().globalData.token,
            cart_id:this.data.cart_id,
            useraddress: this.data.address.id
        }
      let show = this.data.valueshow
      console.log()
      if(show == 1){
        server.send('/cart/submitorders', 'POST',_params).then(res => {
          if (res.status == 200 && status == 1) {
                wx.setStorageSync('shop_id', this.data.cartList[0].seller_info.id)
                wx.navigateTo({
                    url: '../payment/payment'
                })
            } else {
                wx.showToast({
                    title: '暂时不能购买',
                    icon:'none'
                })
            }
        })
      }else{
        wx.showToast({
          title: '请同意免责声明',
          icon:'none'
        })
      }
    }
})